/**
 * Java Enterprise in a Nutshell, Third Edition,
 * by Jim Farley and William Crawford
 * with Prakash Malani, John G. Norman, and Justin Gehtland.
 * Copyright 2006 O'Reilly Media, Inc., 0-596-10142-2.
 *
 * Modified by Vladimir Tarasov, 2009-08-26.
 * Tested with JDK 1.6.
 *
 * To run this (Account) example, build the project and copy .class
 * files in the tavl_rmi\account and tavl_rmi\simple
 * folders (under <project>\build\classes) together with the folder structure
 * (i.e. tavl_rmi\...) to the lab server and then enter the following command:
 * java tavl_rmi.simple.AccountServer
 *
 * If you want to run the example locally, start RMI registry first:
 * rmiregistry.exe
 * then change the port string below and press Shift+F6 to run AccountServer.
 */
package tavl_rmi.simple;

import tavl_rmi.account.AccountImpl;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
/**
 * AccountServer: A utility class that registers an Account with the local
 * RMI registry for remote access
 */
public class AccountServer {

    public static void main(String argv[]) {
        // rmiregistry on the lab server listens to port 1099
        // Note that the protocol defaults to rmi:,
        // the host name defaults to localhost
        final String port = "//:1099/";
        try {
            LocateRegistry.createRegistry(1099);
            System.out.println("Account server is started.");
            // Make two Accounts with given names
            AccountImpl jimAcct = new AccountImpl("JimF");
            AccountImpl billAcct = new AccountImpl("Bill");
            System.out.println(jimAcct.getName()+"\n");
            // Register them with the local naming registry
            try
            {
            Naming.rebind(port + "JimF", jimAcct);
            System.out.println("Registered account as JimF.");
            }
            catch (MalformedURLException a)
            {
                System.out.println("if the name is not an appropriately formatted URL\n" + a.getMessage());
            }
            catch (RemoteException a)
            {
                System.out.println("if registry could not be contacted\n" + a.getMessage());
            }
            Naming.rebind(port + "Bill", billAcct);
            System.out.println("Registered account as Bill.");

            // Keep the server process alive until Q/q is entered
            System.out.println(
                    "To end program, type Q or q, " + "then <return>.");
            char answer = '\0';
            InputStreamReader inputStreamReader =
                    new InputStreamReader(System.in);

            while (!((answer == 'q') || (answer == 'Q'))) {
                try {
                    answer = (char) inputStreamReader.read();
                } catch (IOException e) {
                    System.err.println("I/O exception: " + e.toString());
                }
            }
            System.out.println("Account server is stopped.");
            System.exit(0); // necessary because remote object is created
        } catch (RemoteException re) {
            System.err.println("Remote exception while creating/registering: " + re.getMessage());
        } catch (MalformedURLException mue) {
            System.err.println("Bad name given when binding server object: " + mue.getMessage());
        }
    }
}

